package com.qc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qc.dto.Result;
import com.qc.enity.User;
import com.qc.exception.ResultCodeEnum;
import com.qc.service.IUserService;
import com.qc.service.WeChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private WeChatService weChatService ;
    /**
     * 微信授权登录
     */
    @GetMapping("/login/{secret}")
    public Result<String> authorLogin(@PathVariable String secret) {
        String rows = weChatService.codetoopenid(secret);
        if(rows==null){
            return Result.error(ResultCodeEnum.OID_NULL.getCode(), ResultCodeEnum.OID_NULL.getMsg());
        }
        return Result.successResult(rows);
    }
    /**
    *添加用户
     */
    @GetMapping("/add/{userId}/{oid}/{equipment}/{sex}/{phone}/{area}/{city}/{province}/{createTime}/{createUser}/{modificationTime}/{modificationUser}/{userName}")
    public Result<Void> addUser(@PathVariable Integer userId, @PathVariable String oid, @PathVariable Integer equipment, @PathVariable Integer sex, @PathVariable String phone, @PathVariable String area,
           @PathVariable String city, @PathVariable String province, @PathVariable Date createTime, @PathVariable String createUser, @PathVariable Date modificationTime, @PathVariable String modificationUser ,@PathVariable String userName ){
        int rows=userService.AddUser(userId,oid,equipment,sex,phone, area, city, province, createTime, createUser, modificationTime, modificationUser ,  userName );
        if (rows != 1){
            //添加失败
            return Result.error(ResultCodeEnum.USER_OPERATE_FAIL.getCode(), ResultCodeEnum.USER_OPERATE_FAIL.getMsg());
        }
        return Result.successResultOnlyMessage(ResultCodeEnum.SUCCESS.getMsg());
    }

    /** 根据oid删除用户*/
    @GetMapping("/remove/{oid}")
    public Result<Void> removeUser(@PathVariable String oid){
        int rows=userService.RemoveUser(oid);
        if (rows != 1){
            //添加失败
            return Result.error(ResultCodeEnum.USER_OPERATE_FAIL.getCode(), ResultCodeEnum.USER_OPERATE_FAIL.getMsg());
        }

        return Result.successResultOnlyMessage(ResultCodeEnum.SUCCESS.getMsg());
    }

    /** 根据oid查询用户 */
    @GetMapping("select/{oid}")
    public Result<User> SelectUser(@PathVariable String oid){
        System.out.println(oid);
        if (oid == null) {
            return Result.error(ResultCodeEnum.USER_NULL.getCode(), ResultCodeEnum.USER_NULL.getMsg());
        }
        User user=userService.SelectUser(oid);
        return Result.successResult(user);
    }

    /** 根据uid查询家人 */
    @GetMapping("/selectfam/{uid}")
    public Result<List<User>> SelectFamily(@PathVariable Integer uid){
        List<User> users=userService.SelectFamily(uid);
        if (uid == null) {
            return Result.error(ResultCodeEnum.USER_NULL.getCode(), ResultCodeEnum.USER_NULL.getMsg());
        }
        return Result.successResult(users);
    }
}
